/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgEditComponenti.java
 *
 * Created on 2-ago-2011, 15.52.43
 */
package UI.articoli.dialogs;

import UI.frmArticoli;
import classes.Articoli_Componenti;
import database.DbConnection;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author mrossetti
 */
public class dlgEditComponenti extends javax.swing.JDialog {

    /** Creates new form dlgEditComponenti */
    frmArticoli frmParent;
    JTable TabComp;
    public dlgEditComponenti(java.awt.Frame parent, JTable Tab, boolean modal) {
        super(parent, modal);
        initComponents();
        frmParent=(frmArticoli)parent;
        BoundComponenti();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmbComponenti = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtQta = new javax.swing.JTextField();
        cmdOk = new javax.swing.JButton();
        cmdEsci = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Componente");

        jLabel2.setText("Quantità");

        cmdOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/ok.png.png"))); // NOI18N
        cmdOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOkActionPerformed(evt);
            }
        });

        cmdEsci.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/cancel.png"))); // NOI18N
        cmdEsci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEsciActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbComponenti, 0, 275, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtQta, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmdOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 157, Short.MAX_VALUE)
                        .addComponent(cmdEsci)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbComponenti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtQta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdOk)
                    .addComponent(cmdEsci))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdEsciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEsciActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_cmdEsciActionPerformed

    private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOkActionPerformed
        if(txtQta.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Inserire una quantità", "The WareHouse", JOptionPane.WARNING_MESSAGE);
            return;
        }
        try{
            Articoli_Componenti Componente = new Articoli_Componenti();
            Componente.load(cmbComponenti.getSelectedItem().toString());
            Componente.setQta(Integer.parseInt(txtQta.getText()));
            for(int i = 0;i<frmParent.getArticolo().getComponenti().size();i++){
                if(Componente.getCodice_componente().equals(frmParent.getArticolo().getComponenti().get(i).getCodice_componente())){
                    String msg = "Attenzione!Articolo già presente in lista, sommare la quantità indicata a quella già presente?";
                    if(JOptionPane.showConfirmDialog(this, msg,"The WareHouse",JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION){
                        int CurQta = frmParent.getArticolo().getComponenti().get(i).getQta();
                        CurQta  = CurQta + Componente.getQta();
                        frmParent.getArticolo().getComponenti().get(i).setQta(CurQta);
                        frmParent.BoundComponenti();
                        setVisible(false);
                        dispose();
                        return;
                    }else{
                        setVisible(false);
                        dispose();
                        return;
                    }
                }
            }
            frmParent.getArticolo().getComponenti().add(Componente);
            frmParent.BoundComponenti();
            setVisible(false);
            dispose();
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getMessage(),e.toString(),JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_cmdOkActionPerformed

    private void BoundComponenti(){
        DbConnection Db = new DbConnection();
        ResultSet rs = Db.getResultSet("SELECT codice_componente FROM componenti ORDER BY 1");
        try{
            cmbComponenti.removeAllItems();
            while(rs.next()){
                cmbComponenti.addItem(rs.getString(1));
            }
            rs.close();
            Db.CloseConnection();
        }catch(Exception e){
            e.printStackTrace();
            Db.CloseConnection();
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbComponenti;
    private javax.swing.JButton cmdEsci;
    private javax.swing.JButton cmdOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField txtQta;
    // End of variables declaration//GEN-END:variables
}
